package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.lb.jwitter.service.TblCTDTMonHocService;
import com.lb.jwitter.service.TblCanBoService;
import com.lb.jwitter.service.TblDKMHDotDKService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinNhomLopMonHoc;
import com.lb.jwitter.service.TblThongTinNhomLopMonHocService;
import com.lb.jwitter.service.TblThongTinTKB;
import com.lb.jwitter.service.TblThongTinTKBService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class TblThongTinNhomLopMonHoctblThongTinTKBUpdateAction {

	private int siSoMax;

	public int getSiSoMax() {
		return siSoMax;
	}

	public void setSiSoMax(int siSoMax) {
		this.siSoMax = siSoMax;
	}

	public int getSiSoMin() {
		return siSoMin;
	}

	public void setSiSoMin(int siSoMin) {
		this.siSoMin = siSoMin;
	}

	public int getTuanBatDau() {
		return tuanBatDau;
	}

	public void setTuanBatDau(int tuanBatDau) {
		this.tuanBatDau = tuanBatDau;
	}

	public String getGiangVien() {
		return giangVien;
	}

	public void setGiangVien(String giangVien) {
		this.giangVien = giangVien;
	}

	public String getGhiChu() {
		return ghiChu;
	}

	public void setGhiChu(String ghiChu) {
		this.ghiChu = ghiChu;
	}

    private double phanTramXepLop; 
    public void setPhanTramXepLop(double phanTramXepLop) {
       this.phanTramXepLop = phanTramXepLop;
 }

    public double getPhanTramXepLop() {
       return phanTramXepLop;
 }

    private double phanTramDoi; 
    public void setPhanTramDoi(double phanTramDoi) {
       this.phanTramDoi = phanTramDoi;
 }

    public double getPhanTramDoi() {
       return phanTramDoi;
 }
	private int siSoMin;

	private int tuanBatDau;
	
	
	
	private int tuanKetThuc;


	public int getTuanKetThuc() {
		return tuanKetThuc;
	}

	public void setTuanKetThuc(int tuanKetThuc) {
		this.tuanKetThuc = tuanKetThuc;
	}

	private String giangVien;

	private String ghiChu;

	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private TblThongTinTKBService tblThongTinTKBService;

	private TblThongTinTKB tblThongTinTKB;

	public void setTblThongTinTKB(TblThongTinTKB tblThongTinTKB) {
		this.tblThongTinTKB = tblThongTinTKB;
	}

	private int preUpdate(Message msg, TblThongTinTKB obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postUpdate(Message msg, TblThongTinTKB obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}
	
	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_ThongTin_NhomLop_MonHoc",
					"sua")) {
				return "noprivilege";
			}

			tblThongTinTKBService = (TblThongTinTKBService) context
					.getBean("tbl_ThongTin_TKBClient");

			msg = new Message();
			tblThongTinTKB = tblThongTinTKBService
					.gettblThongTinTKB(new Long(id));
			tblThongTinTKB.setSiSoMax(siSoMax);
			tblThongTinTKB.setSiSoMin(siSoMin);
	        tblThongTinTKB.setPhanTramXepLop( phanTramXepLop); 
	        tblThongTinTKB.setPhanTramDoi(phanTramDoi); 
			tblThongTinTKB.setTuanBatDau(tuanBatDau);
			//tblThongTinTKB.setTuanHoc(Integer.parseInt(tuanHoc, 2));
			tblThongTinTKB.setTuanKetThuc(tuanKetThuc);
			tblThongTinTKB.setGiangVien(giangVien);
			tblThongTinTKB.setGhiChu(ghiChu);

			tblThongTinTKB.setCanBoId(taiKhoanDangNhap.getCanBoId());
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			Date dd = new Date();
			GregorianCalendar c = new GregorianCalendar();
			c.setTime(dd);
			XMLGregorianCalendar d = DatatypeFactory.newInstance()
					.newXMLGregorianCalendar(c);
			tblThongTinTKB.setNgayCapNhat(d);
			preUpdate(msg, tblThongTinTKB);
			if (msg.isPreMessage_b()) {
				tblThongTinTKBService
						.updatetblThongTinTKB(tblThongTinTKB);
				postUpdate(msg, tblThongTinTKB);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}

			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Update tbl_ThongTin_TKB Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
}
